import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_cloud_chat_uikit/ui/views/TIMUIKitConversation/tim_uikit_conversation_last_msg.dart';
import 'package:yqmssa_app/config/app_config.dart';
import 'package:yqmssa_app/router/page_router.dart';
import 'package:yqmssa_app/utils/shared_utils.dart';

import 'message_manager.dart';

class MessageScreen extends StatelessWidget {
  const MessageScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final manager = Get.put(MessageManager());
    return Scaffold(
      backgroundColor: const Color(0XFFF8FAFD),
      appBar: AppBar(
        surfaceTintColor: Colors.white,
        backgroundColor: Colors.white,
        centerTitle: true,
        title: const Text("消息中心"),
        titleTextStyle: const TextStyle(fontSize: 18, color: Colors.black87),
        elevation: 1,
        shadowColor: Colors.grey[100],
      ),
      body: Obx(() {
        return ListView(
          padding: EdgeInsets.zero,
          children: [
            if (manager.massageData.isNotEmpty)
              InkWell(
                onTap: () {
                  manager.cleanUnRead();
                  Get.toNamed(PageRouter.systemMessage);
                },
                child: Obx(() {
                  return Badge(
                    label: Text(
                      "${manager.msgUnread.value}",
                      style: const TextStyle(fontSize: 12),
                    ),
                    isLabelVisible: manager.msgUnread > 0,
                    backgroundColor: const Color(0XFFFF493F),
                    offset: const Offset(-16, -10),
                    alignment: Alignment.centerRight,
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
                      decoration: const BoxDecoration(
                        color: Colors.white,
                        border: Border(bottom: BorderSide(width: .5, color: Color(0XFFE7E7E7))),
                      ),
                      child: Row(
                        children: [
                          Image.asset(AppConfig.imagePath("message_icon.png"), width: 46),
                          const SizedBox(width: 12),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  manager.itemData.value.type,
                                  style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                                ),
                                const SizedBox(height: 6),
                                Text(
                                  manager.itemData.value.messageContent,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: Colors.black.withOpacity(.6)),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                }),
              ),
            if (manager.message.value.conversationID.isNotEmpty && sharedUtils.service != null)
              InkWell(
                onTap: () {
                  if (AppConfig.appInitData.chatBot == 1) {
                    Get.toNamed(PageRouter.intelligenceCustomer);
                  } else {
                    Get.toNamed(PageRouter.humanCustomer);
                  }
                },
                child: Obx(() {
                  return Container(
                    padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
                    decoration: const BoxDecoration(
                      color: Colors.white,
                      border: Border(bottom: BorderSide(width: .5, color: Color(0XFFE7E7E7))),
                    ),
                    child: Row(
                      children: [
                        Image.asset(AppConfig.imagePath("customer_avatar.png"), width: 46),
                        const SizedBox(width: 16),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    "客服消息",
                                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                                  ),
                                  // Text(
                                  //   "${manager.message.value.lastMessage?.timestamp ?? ""}".toDateTime(),
                                  //   style: const TextStyle(fontSize: 12, color: Color(0XFF999999)),
                                  // ),
                                ],
                              ),
                              const SizedBox(height: 8),
                              TIMUIKitLastMsg(
                                lastMsg: manager.message.value.lastMessage,
                                groupAtInfoList: manager.message.value.groupAtInfoList ?? [],
                                draftText: "",
                                context: context,
                                fontSize: 14,
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                }),
              )
          ],
        );
      }),
    );
  }
}
